/**
 * @author yuyangyang
 * @time 2015/2/3
 */
var domain = require('domain'),
    logger = require('./logger').getLogger('default');

module.exports = {
    domain: function (req, res, next) {
        var reqd = domain.create();
        reqd.add(req);
        reqd.add(res);

        reqd.on('error', function (err) {
            res.on('close', function () {
                logger.info('Disposing domain for url ' + req.url);
                reqd.dispose();
            });

            next(err);
        });

        reqd.run(next);
    },
    raise: function (req, res) {

    },
    notFound: function (req, res) {
        var err = new Error('Page Not Found');
        logger.error('Request failed: "' + req.url + '":' + err);
        res.status(404);
        res.render('errors/404', {
            error: err
        });
    }

}